#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

const int N = 5010;
int n, m;
int a[N][N];
int f[N][N];

int main()
{
    int maxNum = -1;
    cin >> n >> m;
    while (n--)
    {
        int i, j, v; cin >> i >> j >> v;
        i++, j++;
        a[i][j] += v;
    }
    for (int i = 1; i <= 5005; i++)
        for (int j = 1; j <= 5005; j++)
            f[i][j] = f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] + a[i][j];
    for (int x2 = m; x2 <= 5005; x2++)
        for (int y2 = m; y2 <= 5005; y2++)
        {
            int x1 = x2 - m + 1, y1 = y2 - m + 1;
            maxNum = max(maxNum, \
                f[x2][y2] - f[x1 - 1][y2] - f[x2][y1 - 1] + f[x1 - 1][y1 - 1]);
        }
    cout << maxNum << endl;
    return 0;
}